<?php
$path = str_replace('cron/notify_admin_user', '', str_replace('\\', '/', getcwd()));

require_once $path . 'config.inc.php';
require_once APP_ROOT_DIR . 'cron/common_cron.php';
require_once APP_ROOT_DIR . 'cron/notify_admin_user/Sender.php';

//if($_SERVER['argv'][1] != 'run') { return; }

$sender = &new Sender();
$last_sent = $sender->readLastRun();
$categories = $sender->getCategories();


$num_sent = 0;
$result = &$sender->getArticles($last_sent);
while($row = $result->FetchRow()) {
	
	$parent_categories = $sender->getParentCategories($row['category_id'], $categories);	
	$subscribers = $sender->getSubscribers(implode(',', $parent_categories));
	
	//echo "<pre>"; print_r($row['id']); echo "</pre>";
	//echo "<pre>"; print_r($parent_categories); echo "</pre>";
	//echo "<pre>"; print_r($subscribers); echo "</pre>";
	
	foreach($subscribers as $k => $v) {
		$sent = $sender->send($sender->getLetter($row, $v), $v);
		if($sent) {
			$num_sent++;
		}
		
		$sender->sleep();	
	}
}

if($num_sent) {
	$sender->writeLastRun();
}

$sender->log('Letters sent: ' . $num_sent);
$sender->log('FINISHED');
?>